package com.example.its2021606134chh.TLModule;

import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.its2021606134chh.HttpUtil;
import com.example.its2021606134chh.R;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: TLightsRViewAdapter$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 17/8/2021$
 * @Version: 1.0
 */
public class TLightsRViewAdapter extends RecyclerView.Adapter<TLightsRViewAdapter.ViewHolder>{
    private static Boolean isButton = false;
    List<Lights> list;
    Context context;
    private static Map<Integer, Boolean> checkStatus = new HashMap<>();
    public TLightsRViewAdapter(List<Lights> list, Context context) {
        this.list = list;
        this.context = context;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_tlm_list,  parent, false);
        return new ViewHolder(view);
    }

    public void selectAll() {
        initCheck(true);
        notifyDataSetChanged();
    }

    public void unSelectAll() {
        initCheck(false);
        notifyDataSetChanged();
    }

    public void initCheck(boolean flag) {
        for (int i = 0; i < list.size(); i++) {
            checkStatus.put(i, flag);
        }
    }

    public static Map<Integer, Boolean> getCheck() {
        return checkStatus;
    }
    public static void setCheck(int position, boolean isChecked) {
        checkStatus.put(position, isChecked);;
    }

    public static Boolean getIsButton() {
        return isButton;
    }
    public static void setIsButton(Boolean b) {
        isButton = b;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Lights lights = list.get(position);
        holder.tvItemRoad.setText(String.valueOf(lights.getRoad()));
        holder.tvItemRed.setText(String.valueOf(lights.getRedLight()));
        holder.tvItemYellow.setText(String.valueOf(lights.getYellowLight()));
        holder.tvItemGreen.setText(String.valueOf(lights.getGreenLight()));
        setCheck(position, holder.tvItemCheck.isChecked());
        holder.btnItemSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(context);
                View view = LayoutInflater.from(context).inflate(R.layout.activity_tlmrv_dialog,null,false);
                dialog.setView(view);
                Button btnTLMDiaCancel = (Button)view.findViewById(R.id.btnTLMDiaCancel);
                EditText edtRedTime = (EditText)view.findViewById(R.id.edtRedTime);
                EditText edtYellowTime = (EditText)view.findViewById(R.id.edtYellowTime);
                EditText edtGreenTime = (EditText)view.findViewById(R.id.edtGreenTime);
                Button btnTLMDiaSave = (Button)view.findViewById(R.id.btnTLMDiaSave);
                AlertDialog mdialog = dialog.show();
                btnTLMDiaSave.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String rood = String.valueOf(position + 1);
                        String sRed = edtRedTime.getText().toString().trim();
                        String sYellow = edtYellowTime.getText().toString().trim();
                        String sGreen = edtGreenTime.getText().toString().trim();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                                    @Override
                                    public void onFailure(Exception e) {

                                    }

                                    @Override
                                    public void onResponse(String json) {
                                        isButton = true;
                                        String msg;
                                        Log.i("TAG", json);
                                        Gson gson = new Gson();
                                        mdialog.dismiss();
                                            TLMResult result = gson.fromJson(json, TLMResult.class);
                                            String RESULT = result.getRESULT();
                                            if (RESULT.equals("S")) {
                                                mdialog.dismiss();
//                                        向数据库传入数据
                                                msg = rood + "路口设置成功\n" + "红灯:" + sRed + "秒\n" + "黄灯:" + sYellow + "秒\n" + "绿灯:" + sGreen + "秒";
                                                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                                            } else {
                                                mdialog.dismiss();
                                                msg = rood + "路口设置失败";
                                                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                                            }
                                    }
                                }, "set_trafficlight_config", new String[]{"TrafficLightId", "RedTime", "GreenTime", "YellowTime", "UserName"}, rood, sRed, sGreen, sYellow, "user1");
                            }
                        }).start();
                    }
                });

                btnTLMDiaCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mdialog.dismiss();
                    }
                });
            }
        });
//        //清除监听器
//        holder.tvItemCheck.setOnCheckedChangeListener(null);
//        //设置选中状态
//        holder.tvItemCheck.setChecked(checkStatus.get(position));
        //再设置一次CheckBox的选中监听器，当CheckBox的选中状态发生改变时，把改变后的状态储存在Map中
        holder.tvItemCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkStatus.put(position, isChecked);
                //check状态一旦改变，保存的check值也要发生相应的变化
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvItemRoad, tvItemRed, tvItemYellow, tvItemGreen;
        CheckBox tvItemCheck;
        Button btnItemSet;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvItemRoad = itemView.findViewById(R.id.tvItemRoad);
            tvItemRed = itemView.findViewById(R.id.tvItemRed);
            tvItemYellow = itemView.findViewById(R.id.tvItemYellow);
            tvItemGreen = itemView.findViewById(R.id.tvItemGreen);
            tvItemCheck = itemView.findViewById(R.id.tvItemCheck);
            btnItemSet = itemView.findViewById(R.id.btnItemSet);
        }
    }
}
